import EnterpriseAdminExtensionAbility from '@ohos.enterprise.EnterpriseAdminExtensionAbility';
import Logger from '../common/logger'
import notificationManager from '@ohos.notificationManager';
import securityManager from '@ohos.enterprise.securityManager';
import { SELF_WANT } from '../common/constants'

export default class EnterpriseAdminAbility extends EnterpriseAdminExtensionAbility {
    static countEnable: number = 0;
    static countDisable: number = 0;
    static countBundleAdd: number = 0;
    static countBundleRemove: number = 0;
    static countBundleStart: number = 0;
    static countBundleStop: number = 0;

    onAdminEnabled() {
        // Ability has back to background
        EnterpriseAdminAbility.countEnable++;
        Logger.info("EnterpriseAdminAbility", "onAdminEnabled countEnable=" + EnterpriseAdminAbility.countEnable +
        ' | countDisable=' + EnterpriseAdminAbility.countDisable)
        this.pushNotification("设备管理器已激活")
        let passwordPolicy: securityManager.PasswordPolicy =
        {
            "validityPeriod": 600000,
            "additionalDescription": "modify 600s"
        }
        try {
            let policy:securityManager.PasswordPolicy = securityManager.getPasswordPolicy(SELF_WANT);
            if (policy.validityPeriod > 0 && policy.validityPeriod < 600000) {
                securityManager.setPasswordPolicy(SELF_WANT, passwordPolicy);
            }
        } catch (err) {
            Logger.info('setPasswordPolicy failed, err.code:' + err.code + ' err.message:' + err.message);
        }
    }

    onAdminDisabled() {
        // Ability has back to background
        EnterpriseAdminAbility.countDisable++;
        Logger.info("EnterpriseAdminAbility", "onAdminDisabled countEnable=" + EnterpriseAdminAbility.countEnable +
        ' | countDisable=' + EnterpriseAdminAbility.countDisable)
        this.pushNotification("设备管理器去激活")
    }

    onBundleAdded(bundleName: string, accountId?: number) {
        console.info(`Succeeded in calling onBundleAdded callback, added bundle name : ${bundleName}, accountId: ${accountId}`);
        this.pushNotification(bundleName + "安装")
    }

    onBundleRemoved(bundleName: string, accountId?: number) {
        console.info(`Succeeded in calling onBundleRemoved callback, removed bundle name : ${bundleName}, accountId: ${accountId}`);
        this.pushNotification(bundleName + "卸载")
    }

    onAppStart(bundleName: string) {
        EnterpriseAdminAbility.countBundleStart++;
        Logger.info("EnterpriseAdminAbility", "onAppStart bundleName" + bundleName + " | onAppStart:" + EnterpriseAdminAbility.countBundleStart + " | onAppStop:" + EnterpriseAdminAbility.countBundleStop)
        this.pushNotification(bundleName + "启动")
    }

    onAppStop(bundleName: string) {
        EnterpriseAdminAbility.countBundleStop++;
        Logger.info("EnterpriseAdminAbility", "onAppStop bundleName" + bundleName + " | onAppStart:" + EnterpriseAdminAbility.countBundleStart + " | onAppStop:" + EnterpriseAdminAbility.countBundleStop)
        this.pushNotification(bundleName + "停止")
    }

    onAccountAdded(accountId: number): void {
        Logger.info("EnterpriseAdminAbility", "onAccountAdded accountId:" + accountId)
        this.pushNotification(accountId + " added")
    }

    onAccountRemoved(accountId: number): void {
        Logger.info("EnterpriseAdminAbility", "onAccountRemoved accountId:" + accountId)
        this.pushNotification(accountId + " removed")
    }

    onAccountSwitched(accountId: number): void {
        Logger.info("EnterpriseAdminAbility", "onAccountSwitched accountId:" + accountId)
        this.pushNotification(accountId + " switch")
    }

    onStart() {
        Logger.info("EnterpriseAdminAbility onStart")
        this.pushNotification("EnterpriseAdminAbility onStart")
    }

    onKioskModeEntering(bundleName: string, userId: number): void {
        Logger.info("EnterpriseAdminAbility", `onKioskModeEntering bundleName:${bundleName}, userId:${userId}`)
        this.pushNotification(`EnterpriseAdminAbility onKioskModeEntering bundleName:${bundleName}, userId:${userId}`);
    }

    onKioskModeExiting(bundleName: string, userId: number): void {
        Logger.info("EnterpriseAdminAbility", `onKioskModeExiting bundleName:${bundleName}, userId:${userId}`)
        this.pushNotification(`EnterpriseAdminAbility onKioskModeExiting bundleName:${bundleName}, userId:${userId}`);
    }

    pushNotification(message:string) {
        let notificationRequest: notificationManager.NotificationRequest = {
            id: 1,
            content: {
                notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
                normal: {
                    title: "EDM通知",
                    text: message,
                    additionalText: "",
                }
            }
        };
        notificationManager.publish(notificationRequest);
    }
};
